package com.example.broadcasttest;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.widget.Button;
import android.widget.Toast;

import androidx.localbroadcastmanager.content.LocalBroadcastManager;

/**
 * Android广播分为标准广播和有序广播
 * TODO:标准广播：完全异步执行的广播，在广播发出后，所有的广播接收器几乎同时接受消息，没有先后顺序可言。
 * TODO:有序广播：一种同步执行的广播，同一时刻只会有一个接收器收到消息，优先级高的广播会首先接受，甚至会打断其他正在传递的广播。
 * <p>
 * 这里实现了动态注册方式监听网络变化、按钮点击发送广播消息两个功能。
 * 利用了夜神模拟器，可以直接运行.
 *
 * @author MAC
 */
public class MainActivity extends Activity {

    private IntentFilter intentFilter;

    private LocalReceiver localReceiver;

    private LocalBroadcastManager localBroadcastManager;

    private IntentFilter intentFilter2;
    private NetWorkChangedReceiver netWorkChangedReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //动态注册实现监听网络状态的变化
        intentFilter2 = new IntentFilter();
        intentFilter2.addAction("android.net.conn.CONNECTIVITY_CHANGE");
        netWorkChangedReceiver = new NetWorkChangedReceiver();
        registerReceiver(netWorkChangedReceiver, intentFilter2);

        //TODO:本地广播的用法并不复杂，主要就是使用了一个 LocalBroadcastManager 来对广播进行管理，并提供了发送广播和注册广播接收器的方法
        //点击按钮发送广播消息
        localBroadcastManager = LocalBroadcastManager.getInstance(this);
        //获取实例
        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(v -> {
            Intent intent = new Intent("com.example.broadcasttest.LOCAL_BROADCAST");
            localBroadcastManager.sendBroadcast(intent);
        });
        intentFilter = new IntentFilter();
        intentFilter.addAction("com.example.broadcasttest.LOCAL_BROADCAST");
        //发送了本地广播，防止被恶意app监听
        localReceiver = new LocalReceiver();
        localBroadcastManager.registerReceiver(localReceiver, intentFilter);
    }

    static class LocalReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            Toast.makeText(context, "received local broadcast", Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(netWorkChangedReceiver);
    }

    class NetWorkChangedReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            //Toast.makeText(context, "network state changed!", Toast.LENGTH_SHORT).show();
            ConnectivityManager connectionManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
            NetworkInfo networkInfo = connectionManager.getActiveNetworkInfo();
            if (networkInfo != null && networkInfo.isAvailable()) {
                Toast.makeText(context, "network is available!", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(context, "network is not available!", Toast.LENGTH_SHORT).show();
            }
        }
    }
}